package com.edu.service.impl;

import com.edu.bean.Writer;
import com.edu.mapper.WriterMapper;
import com.edu.service.BookService;
import com.edu.service.WriterService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author:何包蛋
 * @Date :2022/11/10 - 11 - 10 - 15:36
 */
@Service
@Transactional
public class WriterServiceImpl implements WriterService {
    @Resource
    private WriterMapper writerMapper;
    @Resource
    private BookService bookService;
    @Override
    public void addWriter(Writer writer) {
        Writer writer1 = writerMapper.selectByName(writer.getWritername());
        if (writer1 == null){
            int i = writerMapper.insertSelective(writer);
            if (i==1){
                System.out.println("添加作者成功！！！");
            }else {
                System.out.println("添加作者失败！！！");
            }
        }else {
            System.out.println("作者已存在！！！");
        }
    }

    @Override
    public void delWriter(int writerId) {
        Writer writer = writerMapper.selectByPrimaryKey(writerId);
        if(writer != null){
            int i = writerMapper.deleteByPrimaryKey(writerId);
            if(i != -1 || i != 0){
                System.out.println("作者删除成功！！！");
                bookService.delBook(writerId);
            }else{
                System.out.println("作者删除失败！！！");
            }
        }else{
            System.out.println("作者不存在！！！");
        }
    }

    @Override
    public Writer getWriterById(int writerid) {
        return writerMapper.selectByPrimaryKey(writerid);
    }


}
